<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/9
 * Time: 15:47
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Miaoshagoods extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_start_time = input('start_start_time');
            $start_end_time = input('start_end_time');
            $end_start_time = input('end_start_time');
            $end_end_time = input('end_end_time');
            $painter_keyword = input('painter_keyword');
            $goods_keyword = input('goods_keyword');
            
            $where['goods_status'] = ['NEQ', 3];
            $where['goods_type'] = 3;
            if(isset($goods_keyword) && $goods_keyword != ''){
                $where['goods_id|goods_name'] = ['like', '%' . $goods_keyword . '%'];
            }
            if(isset($painter_keyword) && $painter_keyword != ''){
                $where['goods_user_id|user_username'] = ['like', '%' . $painter_keyword . '%'];
            }
            // 活动开始时间
            if(isset($start_start_time) && $start_start_time != '' && isset($start_end_time) && $start_end_time != ''){
                $where['goods_activity_start_datetime'] = ['between', [$start_start_time, $start_end_time]];
            }
            if(isset($start_start_time) && $start_start_time != '' && isset($start_end_time) && $start_end_time == ''){
                $where['goods_activity_start_datetime'] = ['>', $start_start_time];
            }
            if(isset($start_start_time) && $start_start_time == '' && isset($start_end_time) && $start_end_time != ''){
                $where['goods_activity_start_datetime'] = ['<', $start_end_time];
            }
            // 活动结束时间
            if(isset($end_start_time) && $end_start_time != '' && isset($end_end_time) && $end_end_time != ''){
                $where['goods_activity_end_datetime'] = ['between', [$end_start_time, $end_end_time]];
            }
            if(isset($end_start_time) && $end_start_time != '' && isset($end_end_time) && $end_end_time == ''){
                $where['goods_activity_end_datetime'] = ['>', $end_start_time];
            }
            if(isset($end_start_time) && $end_start_time == '' && isset($end_end_time) && $end_end_time != ''){
                $where['goods_activity_end_datetime'] = ['<', $end_end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods')
                ->join('up_u_user', 'user_id = goods_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods')
                ->join('up_u_user', 'user_id = goods_user_id', 'LEFT')
                ->field('goods_id, goods_name, goods_user_id, user_username, goods_activity_start_datetime, 
                goods_activity_end_datetime, goods_sta')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('goods_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 评价列表
     * @return mixed|\think\response\Json
     */
    public function goodsComment(){
        if(request()->isAjax ()){
            $gc_type = input('gc_type'); // 1 好评   2 差评
            
            $where['gc_goods_id'] = session('goods_id');
            if(isset($gc_type) && $gc_type != ''){
                $where['gc_type'] = $gc_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_comment')
                ->join('up_u_user', 'gc_user_id = user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_comment')
                ->join('up_u_user', 'gc_user_id = user_id', 'LEFT')
                ->field('gc_id, user_username, gc_type, gc_star, gc_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('gc_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $goods_id = input('goods_id');
        if(isset($goods_id) && $goods_id != ''){
            session('goods_id', $goods_id);
        }
        return $this->fetch('miaoshagoods/goodscomment');
    }
    
    /**
     * 评价详情
     * @return mixed
     */
    public function commentDetail(){
        $gc_id = input('gc_id');
        $detail = Db::table('up_goods_comment')
            ->join('up_goods_spec', 'gc_spec_id = spec_id', 'LEFT')
            ->join('up_u_user', 'gc_user_id = user_id', 'LEFT')
            ->where('gc_id', $gc_id)
            ->field('user_username, spec_name, spec_can, gc_star, gc_type, gc_content, gc_images, gc_time')
            ->find();
        $images = trim($detail['gc_images'], ',');
        if(!empty($images)){
            $detail['gc_images'] = explode(',', $images);
        }else{
            $detail['gc_images'] = [];
        }
        return $this->fetch('miaoshagoods/commentdetail', ['detail' => $detail]);
    }
    
    /**
     * 秒杀商品规格
     * @return mixed|\think\response\Json
     */
    public function goodsSpec(){
        if(request()->isAjax ()){
            $where['spec_goods_id'] = session('goods_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_spec')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_spec')
                ->field('spec_name, spec_price, spec_old_price, spec_can, spec_stock, spec_sail_num')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $goods_id = input('goods_id');
        if(isset($goods_id) && $goods_id != ''){
            session('goods_id', $goods_id);
        }
        return $this->fetch('miaoshagoods/goodsspec');
    }
    
    /**
     * 编辑活动场次时间
     * @return mixed|\think\response\Json
     */
    public function goodsEdit(){
        $goods_id = input('goods_id');
        if(Request::instance()->isPost()){
            $goods_activity_date = input('goods_activity_date');
            $goods_activity_time = input('goods_activity_time');
            $timeArr = explode('-', $goods_activity_time);
            $goods_activity_start_time = $timeArr[0];
            $goods_activity_end_time = $timeArr[1];
            
            $goods = Db::table('up_goods')
                ->where('goods_id', $goods_id)
                ->field('goods_activity_date, goods_activity_start_time, goods_activity_end_time, goods_sta')
                ->find();
            if($goods['goods_sta'] == 2){
                return json(['code' => 100, 'msg' => '活动已开始，不允许编辑']);
            }
            if($goods['goods_sta'] == 3){
                return json(['code' => 100, 'msg' => '活动已结束，不允许编辑']);
            }
            if($goods['goods_activity_date'] == $goods_activity_date && $goods['goods_activity_start_time'] == $goods_activity_start_time && $goods['goods_activity_end_time'] == $goods_activity_end_time){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                // 限制可以参加的商品数量
                $limit_num = Db::table('up_activity_times')
                    ->where('at_type', 3)
                    ->where('at_start_time', $goods_activity_start_time)
                    ->where('at_end_time', $goods_activity_end_time)
                    ->value('at_num');
                // 已参加商品数
                $already_num = Db::table('up_goods')
                    ->where('goods_type', 3)
                    ->where('goods_activity_date', $goods_activity_date)
                    ->where('goods_activity_start_time', $goods_activity_start_time)
                    ->where('goods_activity_end_time', $goods_activity_end_time)
                    ->count();
                if($already_num >= $limit_num){
                    return json(['code' => 100, 'msg' => '该场次活动名额已满']);
                }else{
                    $update = [
                        'goods_activity_date' => $goods_activity_date,
                        'goods_activity_start_time' => $goods_activity_start_time,
                        'goods_activity_end_time' => $goods_activity_end_time,
                        'goods_activity_start_datetime' => $goods_activity_date . ' ' . $goods_activity_start_time,
                        'goods_activity_end_datetime' => $goods_activity_date . ' ' . $goods_activity_end_time
                    ];
                    $res = Db::table('up_goods')->where('goods_id', $goods_id)->update($update);
                    if($res){
                        return json(['code' => 200, 'msg' => '编辑成功']);
                    }else{
                        return json(['code' => 100, 'msg' => '编辑失败']);
                    }
                }
            }
        }
        $goods = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_activity_date, goods_activity_start_time, goods_activity_end_time')
            ->find();
        $goods['activity_time'] = $goods['goods_activity_start_time'] . '-' . $goods['goods_activity_end_time'];
        return $this->fetch('miaoshagoods/goodsedit', ['goods' => $goods, 'goods_id' => $goods_id]);
    }
    
    /**
     * 删除商品
     * @return \think\response\Json
     */
    public function goodsDel(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}